package com.yoomiito.app.widget.expand

import android.app.Activity
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.router.Router
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.WebViewActivity
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.expand_view_layout.view.*

class ExpandView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
    //    private val sddRights = ConfigFileUtils.sendMoreRights(context)
//    val icons = arrayOf(
//            R.drawable.icon_right_1,
//            R.drawable.icon_right_2,
//            R.drawable.icon_right_3,
//            R.drawable.icon_right_4,
//            R.drawable.icon_right_5,
//            R.drawable.icon_right_6,
//            R.drawable.icon_right_7,
//            R.drawable.icon_right_8,
//            R.drawable.icon_right_9,
//            R.drawable.icon_right_10
//    )
//    val content = arrayOf("免费礼品", "补贴奖励","推广奖励","直播特权","线上门店", "自购省钱", "推广赚钱","专属客服","活动福利","学习成长")
//    val content2 = arrayOf(
//            "获得100次免费包邮赠送好友礼品机会",
//            "每送出一份商品得10元返佣",
//            "每推广1个送多多VIP奖礼包10%佣金",
//            "获价值1280元直播端口开通特权",
//            "获得开通给上门店资格，全平台帮你销售商品",
//            "自己购买商品，最高省80%",
//            "推荐朋友购物，获得相应佣金",
//            "专属客服经理，1对1专属服务",
//            "每年5场会员专享免单活动",
//            "每周线上商学院，手把手教您赚到100万"
//    )
    init {
//        val data = mutableListOf<ExpandData>()
//        icons.forEachIndexed { index, i ->
//            data.add(ExpandData(i,content[index],content2[index],index==1||index==2))
//        }
        val sddRights = ConfigFileUtils.sendMoreRights(context)
        val view = LayoutInflater.from(context).inflate(R.layout.expand_view_layout, null, false)
        val lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        addView(view, lp)
        view.findViewById<View>(R.id.bg_3).background = ColorUtils.gradient("#AE7750", "#FFFFFF", GradientDrawable.Orientation.TOP_BOTTOM)
        childView.showViewSize = 3
        childView.setData(sddRights)
        detailRightTv.background = ColorUtils.gradientAndRadius("#FFFF246C", "#FFFF035B", 12f)
        detailRightTv.setOnClickListener {
            Router.newIntent(context as Activity)
                    .to(WebViewActivity::class.java)
                    .putInt(Constant.TYPE, Const.Url.SddVipRights)
                    .launch()
        }
        indicator.setOnClickListener {
            indicatorTv.text = if (childView.isExpand) "查看更多权益" else "收起权益"
//            detailRightTv.visibility = if (childView.isExpand) View.VISIBLE else View.GONE
            childView.expand(!childView.isExpand)
            anim(indicatorIcon, if (childView.isExpand) 180f else 0f, if (childView.isExpand) 360f else 180f)
        }
    }

    private fun anim(view: ImageView, fromDegrees: Float, toDegrees: Float) {
        val anim = RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        anim.interpolator = LinearInterpolator()
        anim.fillAfter = true
        anim.duration = 400
        anim.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                if (toDegrees == 360f) view.clearAnimation()
            }

            override fun onAnimationStart(animation: Animation?) {
            }

        })
        view.startAnimation(anim)
    }
}